//
//  CustomTool.m
//  JFTwxsd
//
//  Created by Steven on 2018/9/14.
//  Copyright © 2018年 junfeiweiyejiaoyujituan. All rights reserved.
//

#import "CustomTool.h"
#import "MJRefresh.h"

@implementation UIScrollView (Refresh)
- (void)pullRefresh:(dispatch_block_t)block{
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        !block ?: block();
    }];
}


- (void)downRefresh:(dispatch_block_t)block{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        !block ?: block();
    }];
}
@end



@implementation UITextField (Icon)

- (void)setIconwithDir:(DIR)dir icon:(UIImage *)icon{
    
    if (dir == Left) {
       self.leftViewMode = UITextFieldViewModeAlways;
    }else{
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    
    //图标父View
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(/**10*/0, 0,self.frame.size.height + /**10*/0, self.frame.size.height)];
    UIImageView *leftImageView = [[UIImageView alloc]initWithImage:icon];
    [leftImageView sizeToFit];
    leftImageView.center = CGPointMake(leftView.frame.size.width/2, leftView.frame.size.height/2);
    [leftView addSubview:leftImageView];
    
    if (dir == Left) {
        self.leftView = leftView;
    }else{
        self.rightView = leftView;
    }
    
}

@end

#import <objc/message.h>
@implementation NSBundle (Version)
- (NSString *)shortVersion{
    NSString *shortVersion = objc_getAssociatedObject(self, "Bundle.shortVersion");
    if (shortVersion == nil) {
        shortVersion = self.infoDictionary[@"CFBundleShortVersionString"];
    }
    return shortVersion;
}

- (NSString *)version{
    NSString *version = objc_getAssociatedObject(self, "Bundle.version");
    if (version == nil) {
        version = self.infoDictionary[@"CFBundleVersion"];
    }
    
    return version;
}

@end


@concreteprotocol(Empty)

- (void)setDelegate:(UIScrollView *)scrollView{
    scrollView.emptyDataSetDelegate = self;
    scrollView.emptyDataSetSource = self;
}
@end

@implementation UIViewController (Empty)


- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无数据";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
@end

#pragma mark - stroyBoardName
@implementation UIViewController (storyBoardName)
+ (NSString *)storyboardName{
    NSString *name = [NSStringFromClass([self class]) componentsSeparatedByString:@"."].lastObject;
    return name;
}
@end

#pragma mark - LoadNibName
@implementation UIView (LoadNibName)
//- (void)setNibViewName:(NSString *)nibViewName{ }
//- (NSString *)nibViewName{
//    NSString *name = objc_getAssociatedObject(self, "UIView.nibName");
//    if (name == nil) {
//        name = [NSStringFromClass([self class]) componentsSeparatedByString:@"."].lastObject;
//    }
//    return name;
//}
//+ (NSString *)nibViewName{
//    return [NSStringFromClass([self class]) componentsSeparatedByString:@"."].lastObject;
//}

+ (UIView *)nibView {
    UIView *view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].lastObject;
    return view;
}

+ (NSString *)nibName {
    NSString *name = NSStringFromClass([self class]);
    return name;
}

@end

#pragma mark - 导航栏下的线隐藏
@implementation UIView (HiddenLine)

- (void)setNavBarHairlineImageView:(UIImageView *)navBarHairlineImageView{ }

- (UIImageView *)navBarHairlineImageView{
    UIImageView *navLine = objc_getAssociatedObject(self, "UINavigationBar.line");
    
    if (navLine == nil) {
        
        if ([self isKindOfClass:UIImageView.class]) {
            return (UIImageView *)self;
        }
        
        for (UIView *subView in self.subviews) {
            UIImageView *imageView = [subView navBarHairlineImageView];
            if (imageView) {
                return imageView;
            }
        }
    }
    return nil;
}
@end

#pragma mark - 行间距和字间距
@implementation UILabel (ChangeLineSpaceAndWordSpace)
//行间距
- (void)changeLineSpace:(float)space{
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = space;
    [attri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.text.length)];
    self.attributedText = attri;
    [self sizeToFit];
}
//字间距
- (void)changeWordSpace:(float)space{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:self.text attributes:@{NSKernAttributeName:@(space)}];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    [attri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.text.length)];
    self.attributedText = attri;
    [self sizeToFit];
}

- (void)changeLineSpace:(float)lineSpace wordSapce:(float)wordSpace{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:self.text attributes:@{NSKernAttributeName:@(wordSpace)}];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = lineSpace;
    [attri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.text.length)];
    self.attributedText = attri;
    [self sizeToFit];
    
}

@end

@implementation UIButton (CountDown)

- (void)coutDown:(NSInteger)time{
    self.enabled = NO;
    
    UIColor *defaultColor = self.backgroundColor;
    //设置倒计时的背景色
    self.backgroundColor = UIColor.lightGrayColor;
    
    __block NSInteger remainingCount = time;

    //触发
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));

    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    
    dispatch_source_set_event_handler(timer, ^{
        // 返回主线程处理一些事件，更新UI等等
        if (remainingCount <= 0) {
            //倒计时关闭
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                self.backgroundColor = defaultColor;
                self.enabled = YES;
                [self setTitle:@"获取验证码" forState:UIControlStateNormal];
            });
            
        }else{
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setTitle:[NSString stringWithFormat:@"重新发送(%ld)", remainingCount] forState:UIControlStateNormal];
            });
            
        }
        remainingCount--;
    });
    //启动时间源
    dispatch_resume(timer);
 
}

@end
















